package com.youboy.wechatapi.utils.text;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.youboy.wechatapi.domain.enums.SearchType;
import com.youboy.wechatapi.domain.message.Article;
import com.youboy.wechatapi.utils.wechat.WechatApi;
import com.youboy.wechatapi.utils.wechat.YouboyApi;

/**
 * @ProjectName：wechatapi   
 * @author：冉尧（Yvan）
 * @CreateTime：2014年10月14日 上午11:08:35  
 * @Remarks：   
 */
public class MsgUtils {

	//自动回复内容
	public static Map<String, String> msgMap = new HashMap<String, String>();
	
	/**
	 * 临时
	 * 活动内容
	 */
	public static Map<String, List<Article>> activeMap = new HashMap<String, List<Article>>();
	
	public static Map<String, SearchType> searchTypeMap = new HashMap<String, SearchType>();
	static{
		/*String help = getHelpMsg();
		msgMap.put("求助", help);
		msgMap.put("帮助", help);
		msgMap.put("?", help);
		msgMap.put("？", help);
		msgMap.put("help", help);*/
		
		String agent = "代理详情咨询请联系:\n戴先生:18620565656\n彭先生:13825071722";
		msgMap.put("代理", agent);
		msgMap.put("加盟", agent);
		msgMap.put("加盟代理", agent);
		msgMap.put("代理加盟", agent);
		
		String adv = getAdvertMsg();
		msgMap.put("广告", adv);
		msgMap.put("广告服务", adv);
		msgMap.put("投放广告", adv);
		
		/*String interview = "欢迎您的咨询，招聘相关事项请致电我司人事部热线：\n020-62308753，或咨询一呼百应招聘QQ：\n469196848，感谢您的关注！";
		msgMap.put("面试", interview);
		msgMap.put("求职", interview);
		msgMap.put("招聘", interview);*/
		
		activeMap.put("1", getRecruitmentInfo());
		activeMap.put("【1】", getRecruitmentInfo());
		activeMap.put("招聘职位", getRecruitmentInfo());
		activeMap.put("产品", getRecruitmentInfo());
		activeMap.put("设计", getRecruitmentInfo());
		activeMap.put("工程师", getRecruitmentInfo());
		activeMap.put("搜索", getRecruitmentInfo());
		activeMap.put("数据库", getRecruitmentInfo());
		activeMap.put("监控", getRecruitmentInfo());
		activeMap.put("测试BI", getRecruitmentInfo());
		activeMap.put("运营", getRecruitmentInfo());
		activeMap.put("推广", getRecruitmentInfo());
		activeMap.put("专员", getRecruitmentInfo());
		activeMap.put("招聘", getRecruitmentInfo());
		activeMap.put("面试", getRecruitmentInfo());
		activeMap.put("求职", getRecruitmentInfo());
		
		searchTypeMap.put("1", SearchType.INTERVIEW);
		searchTypeMap.put("【1】", SearchType.INTERVIEW);
		searchTypeMap.put("招聘职位", SearchType.INTERVIEW);
		searchTypeMap.put("产品", SearchType.INTERVIEW);
		searchTypeMap.put("设计", SearchType.INTERVIEW);
		searchTypeMap.put("工程师", SearchType.INTERVIEW);
		searchTypeMap.put("搜索", SearchType.INTERVIEW);
		searchTypeMap.put("数据库", SearchType.INTERVIEW);
		searchTypeMap.put("监控", SearchType.INTERVIEW);
		searchTypeMap.put("测试BI", SearchType.INTERVIEW);
		searchTypeMap.put("运营", SearchType.INTERVIEW);
		searchTypeMap.put("推广", SearchType.INTERVIEW);
		searchTypeMap.put("专员", SearchType.INTERVIEW);
		searchTypeMap.put("招聘", SearchType.INTERVIEW);
		searchTypeMap.put("面试", SearchType.INTERVIEW);
		searchTypeMap.put("求职", SearchType.INTERVIEW);
		
		activeMap.put("2", getActiveInfo());
		activeMap.put("【2】", getActiveInfo());
		activeMap.put("推荐", getActiveInfo());
		activeMap.put("活动", getActiveInfo());
		
		searchTypeMap.put("2", SearchType.ACTIVE);
		searchTypeMap.put("【2】", SearchType.ACTIVE);
		searchTypeMap.put("推荐", SearchType.ACTIVE);
		searchTypeMap.put("活动", SearchType.ACTIVE);
		
		
		String customer = "对不起，人工服务服务暂未开通，欢迎拨打客服电话\n020-62307188";
		msgMap.put("人工", customer);
		msgMap.put("客服", customer);
		msgMap.put("客服服务", customer);
		msgMap.put("人工服务", customer);
		
		String love = "/玫瑰，I Love You too !";
		msgMap.put("i love you", love);
		msgMap.put("我爱你", "/玫瑰，我也爱你！");
		msgMap.put("你好", "/玫瑰，你好。");
		msgMap.put("我讨厌你", "/委屈，为什么讨厌我？");
		
		String complaints = "您的投诉已收到，我们将会认真处理，感谢您对一呼百应的帮助。";
		msgMap.put("投诉", "要投诉，请输入\n“投诉：具体的投诉内容” ");
		msgMap.put("投诉deal", complaints);
		
		String ideaDeal = "您好，非常感谢您对我司提出的宝贵意见，我们会不断改进服务，争取做到令您满意！";
		msgMap.put("意见", "提意见，请输入\n“意见：具体的意见”");
		msgMap.put("建议", "提建议，请输入\n“建议：具体的建议”");
		msgMap.put("反馈", "提反馈，请输入\n“反馈：具体的反馈”");
		msgMap.put("问题", "反映问题，请输入\n“问题：具体的问题” ");
		msgMap.put("意见deal", ideaDeal);
		msgMap.put("建议deal", ideaDeal);
		msgMap.put("反馈deal", ideaDeal);
		msgMap.put("问题deal", complaints);
	}
	
	
	private static String getAdvertMsg(){
		StringBuilder contentMsg = new StringBuilder();
		contentMsg.append("提供在一呼百应旗下各频道站点上的图片和文字链接等广告投放展示业务。\n\n");
		contentMsg.append("所有位置广告素材可以使用\nSWF、GIF、JPG等格式，");
		contentMsg.append("\n一般广告尺寸格式为\n960*90、960*60、728*90、728*60、300*250、250*250、200*200、文字链或其他形式广告，");
		contentMsg.append("也可自定义尺寸，和自由选择投放放置。");
		contentMsg.append("\n\n咨询热线： 13556123015");
		contentMsg.append("\nQQ：2820010589");
		contentMsg.append("\n电子邮箱：\n2820010589@qq.com");
		return contentMsg.toString();
	}
	
	/**
	 * 获取招聘信息
	 * @param key
	 */
	private static List<Article> getRecruitmentInfo(){
		List<Article> articleList = null;
		Article article = null;
		
		articleList = new ArrayList<Article>();
		String url = null;
		article = new Article();
		article.setTitle("一、产品类");
		article.setDescription("");
		article.setPicUrl("http://mmbiz.qpic.cn/mmbiz/5z5aMu4v9ZiceCMcsBEG9qd3Sa7OvCp56DDJZpolnqt9RwdOic3F3yL177l98khuvibIgvybRhpibChKjYgvRNu3gw/0");
		url = "http://mp.weixin.qq.com/s?__biz=MjM5MDY4MjM0MQ==&mid=201992978&idx=1&sn=6e50fde81f39976ac19149ed47e6de15#rd";
		YouboyApi.apiMap.put(101, url);
		article.setUrl(getOauthUrl(url));
		articleList.add(article);
		
		article = new Article();
		article.setTitle("二、设计类");
		article.setDescription("");
		article.setPicUrl("http://mmbiz.qpic.cn/mmbiz/5z5aMu4v9ZiceCMcsBEG9qd3Sa7OvCp56kwIsFia6hOTLRy9xqLH6LPCjIYEj1vmXk6QAQia1R57yur38d0ISdGmw/0");
		url = "http://mp.weixin.qq.com/s?__biz=MjM5MDY4MjM0MQ==&mid=201992978&idx=2&sn=f3ef678d206ee9a0ed981a64f2f43d11#rd";
		YouboyApi.apiMap.put(102, url);
		article.setUrl(getOauthUrl(url));
		articleList.add(article);
		
		article = new Article();
		article.setTitle("三、JAVA开发类");
		article.setDescription("");
		article.setPicUrl("http://mmbiz.qpic.cn/mmbiz/5z5aMu4v9ZiceCMcsBEG9qd3Sa7OvCp56MWlKFJeqDA69BwYPTEicMVttUAAyd5icvBvQJPHLiaPUJcA6yKCAksVZQ/0");
		url = "http://mp.weixin.qq.com/s?__biz=MjM5MDY4MjM0MQ==&mid=201992978&idx=3&sn=98d4da92f04553b8ab47531f9593306d#rd";
		YouboyApi.apiMap.put(103, url);
		article.setUrl(getOauthUrl(url));
		articleList.add(article);
		
		article = new Article();
		article.setTitle("四、.NET开发类");
		article.setDescription("");
		article.setPicUrl("http://mmbiz.qpic.cn/mmbiz/5z5aMu4v9ZiceCMcsBEG9qd3Sa7OvCp561IqNpuGADicmt9tibgcCRywrNMpf6IyAoWwic8Gw9ZLic46BANTHiceaeuw/0");
		url = "http://mp.weixin.qq.com/s?__biz=MjM5MDY4MjM0MQ==&mid=201992978&idx=4&sn=c1036e371764b8461e7b38edf34f2c15#rd";
		YouboyApi.apiMap.put(104, url);
		article.setUrl(getOauthUrl(url));
		articleList.add(article);
		
		article = new Article();
		article.setTitle("五、其它工程师类");
		article.setDescription("");
		article.setPicUrl("http://mmbiz.qpic.cn/mmbiz/5z5aMu4v9ZiceCMcsBEG9qd3Sa7OvCp56vecmsIU6UTGEibchlfm98qbvJ5IvfIiaq0zOic8KHTgzdgTAU8oFYcx7Q/0");
		url = "http://mp.weixin.qq.com/s?__biz=MjM5MDY4MjM0MQ==&mid=201992978&idx=5&sn=b855a925b2fd4da56a99cf6e902041bf#rd";
		YouboyApi.apiMap.put(105, url);
		article.setUrl(getOauthUrl(url));
		articleList.add(article);
		
		article = new Article();
		article.setTitle("六、数据分析类");
		article.setDescription("");
		article.setPicUrl("http://mmbiz.qpic.cn/mmbiz/5z5aMu4v9ZiceCMcsBEG9qd3Sa7OvCp56IJBzPNEZT1iaicia2d9l9sMhLWJtkHSCSbLGicGiaURTPyibca7aQTnVBWpQ/0");
		url = "http://mp.weixin.qq.com/s?__biz=MjM5MDY4MjM0MQ==&mid=201992978&idx=6&sn=c95be846f11cb7e218facc5f6b923879#rd";
		YouboyApi.apiMap.put(106, url);
		article.setUrl(getOauthUrl(url));
		articleList.add(article);
		
		article = new Article();
		article.setTitle("七、运营推广类");
		article.setDescription("");
		article.setPicUrl("http://mmbiz.qpic.cn/mmbiz/5z5aMu4v9ZiceCMcsBEG9qd3Sa7OvCp56Dh0uG49U7dQRM0sElQLxFRIe50DvgVcUflxIBrbPJKxicM64czkfDLQ/0");
		url = "http://mp.weixin.qq.com/s?__biz=MjM5MDY4MjM0MQ==&mid=201992978&idx=7&sn=9872410f81b61ebcbf74035af7f808f0#rd";
		YouboyApi.apiMap.put(107, url);
		article.setUrl(getOauthUrl(url));
		articleList.add(article);
		
		article = new Article();
		article.setTitle("八、招聘类");
		article.setDescription("");
		article.setPicUrl("http://mmbiz.qpic.cn/mmbiz/5z5aMu4v9ZiceCMcsBEG9qd3Sa7OvCp56CpHASoV7ZpiaibQNQH5icLkJFoZE1W6kDS0pBlkSialzBibPAibGesRsvo6A/0");
		url = "http://mp.weixin.qq.com/s?__biz=MjM5MDY4MjM0MQ==&mid=201992978&idx=8&sn=55939efb2284ccbd8b189cb8ff5dcfe8#rd";
		YouboyApi.apiMap.put(108, url);
		article.setUrl(getOauthUrl(url));
		articleList.add(article);
		
		return articleList;
	}
	
	private static String getOauthUrl(String url){
		StringBuilder oauthUrl = new StringBuilder();
        try {
        	String uri = URLEncoder.encode(url, "utf-8");
        	oauthUrl.append("https://open.weixin.qq.com/connect/oauth2/authorize?appid=").append(WechatApi.APPID)
					.append("&redirect_uri=")
					.append(URLEncoder.encode(WechatApi.URL+"/help/wechatCallBack?redirect_url="+uri, "utf-8"))
					.append("&response_type=code")
					.append("&scope=snsapi_base&state=a#wechat_redirect");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        return oauthUrl.toString();
	}
	
	/**
	 * 获取活动信息
	 * @param key
	 */
	private static List<Article> getActiveInfo(){
		List<Article> articleList = new ArrayList<Article>();
		Article article = new Article();
		article.setTitle("【推荐人才得话费】一呼百应人才召集令，三重大礼等着您！");
		article.setDescription("为给客户提供更好的产品与服务，一呼百应在此发出人才召集令，三重大礼等着您！");
		article.setPicUrl("http://mmbiz.qpic.cn/mmbiz/5z5aMu4v9ZiceCMcsBEG9qd3Sa7OvCp56wfn3qzTuOJwudTUWsUibbmHjmu7bL73fp0Otichalicu3sKAjTEFzkElQ/0");
		
		String url = "http://mp.weixin.qq.com/s?__biz=MjM5MDY4MjM0MQ==&mid=201993104&idx=1&sn=553cbad4c03015647ffa6fdcde3543d8#rd";
		YouboyApi.apiMap.put(109, url);
		article.setUrl(getOauthUrl(url));
		
		articleList.add(article);
		return articleList;
	}
	
	private static String setCallBackUrl(String openid, String redirectUrl) throws UnsupportedEncodingException{
		StringBuilder url = new StringBuilder();
		url.append("http://wechatapi.youboy.com/help/callBack?openid=").append(openid)
				.append("&redirect_url=").append(URLEncoder.encode(redirectUrl, "utf-8"));
		return url.toString();
	}
	
	/**
	 * 获取帮助信息
	 * @param isSearch 是否已经输入搜索过了
	 * @param isBind 是否已经绑定
	 * @param wechatUrl 跳转地址
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String getHelpMsg(Boolean isInput, Boolean isBind, String openid, String wechatUrl) throws UnsupportedEncodingException {
		
		StringBuilder helpMsg = new StringBuilder();
		if(isInput == null){
			String url = setCallBackUrl(openid, "http://m.youboy.com");
			helpMsg.append("您好！欢迎关注‘<a href=\"").append(url).append("\">一呼百应</a>’\n");
			url = setCallBackUrl(openid, "http://www.youboy.com");
			helpMsg.append("官网：\n<a href=\"").append(url).append("\">www.youboy.com</a>，\n");
			helpMsg.append("服务热线： 020-62307188\n");
			if(isBind){
				helpMsg.append("<a href=\"" + wechatUrl + "\">点击这里进入您的商铺</a>\n");
			}else{
				helpMsg.append("<a href=\"" + wechatUrl + "\">点击这里绑定商铺</a>，实时获知店铺留言、商机等信息。\n");
			}
			helpMsg.append("搜商品：输入“商品：关键词”").append("\n");
			helpMsg.append("搜商家：输入“商家：关键词”").append("\n");
			helpMsg.append("提建议：输入“建议：具体建议”").append("\n");
		}else {
			if(isInput){
				helpMsg.append("温馨提示：无法识别您的输入，");
			}
			
			helpMsg.append("请按照下面的方式进行操作：").append("\n");
			
			helpMsg.append("1、搜商品：输入“商品：关键词”").append("\n");
			helpMsg.append("2、搜商家：输入“商家：关键词”").append("\n");
			helpMsg.append("3、提建议：输入“建议：具体建议”").append("\n");
			if(isBind){
				helpMsg.append("4、<a href=\"" + wechatUrl + "\">点击这里进入您的商铺</a>\n");

				//helpMsg.append("5、点击这里进入卖家商机").append("\n");
				//helpMsg.append("6、点击这里进入买家商机").append("\n");
			}else{
				helpMsg.append("4、<a href=\"" + wechatUrl + "\">点击这里绑定商铺</a>，实时获知店铺留言、商机等信息。\n");
			}
		}
		
		/*helpMsg.append("回复【1】：查看招聘职位\n");
		helpMsg.append("回复【2】：查看推荐有礼活动\n");*/
		helpMsg.append("发现更多功能，火速猛戳底部菜单。");
		return helpMsg.toString();
	}
	
}

